﻿singleApp.controller('machineGroupCtrl', ['$http', '$scope', 'PageService', function ($http, $scope, PageService) {
    $scope.list = [];
    $scope.model_add_new = { id: 0, name: "" };

    //Init
    $scope.init = function () {
        layer.load(2);

        var url = window.singleConfig.rootUrl + "/api/admin/MachineGroupService/getAll";
        $http.get(url).then(function (result) {
            $scope.list = result.data.data;
            layer.closeAll();
        }, function () { });
    };

    $scope.init();

    $scope.showAddModel = function () {
        $scope.model_add_new.id = 0;
        $scope.model_add_new.name = "";

        layer.open({
            type: 1,
            title: "添加",
            area: ['400px', '190px'], //宽高
            content: $("#divAddModelPopup")
        });
    };

    $scope.editModel = function (item) {
        $scope.model_add_new.id = item.id;
        $scope.model_add_new.name = item.name;

        layer.open({
            type: 1,
            title: "编辑",
            area: ['400px', '190px'], //宽高
            content: $("#divAddModelPopup")
        });
    };

    $scope.confirmAddModel = function () {
        layer.load(2);

        var url = window.singleConfig.rootUrl + "/api/admin/MachineGroupService/modify";
        $.post(url, $scope.model_add_new, function (result) {
            layer.closeAll();
            $scope.init();
        }, "json");
    };

    $scope.delete = function (item) {
        layer.confirm("确认删除该分组吗？", {
            btn: ['确认', '取消']
        }, function () {
            layer.load(2);

            var url = window.singleConfig.rootUrl + "/api/admin/MachineGroupService/delete?id=" + item.id;
            $http.get(url).then(function (result) {
                layer.closeAll();
                $scope.init();
            }, function () { });
        }, function () {

        });
    };
    
}]);